
//% color="#2c92ff" iconWidth=50 iconHeight=40
    namespace mindRNN{
        
    //% block="文本数据处理" blockType="tag"
    export function tagmyBlock18() {
    }
    //% block="初始化" blockType="command"
    export function myBlock2(parameter: any, block: any) {
        Generator.addImport(`import json
from pathlib import Path
import copy

import pandas as pd
import numpy as np
import jieba
from sklearn.model_selection import train_test_split
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchtext.vocab import build_vocab_from_iterator
import tkinter as tk
from tkinter import messagebox
import os
from collections import OrderedDict
from traininit import *
from preinit import *`)
        Generator.addInit("myBlock9670",`print("正在初始化中......")
# 初始化分析器
analyzer = SentimentAnalyzer()
predictor = SentimentPredictor()`)
    }
    //% block="加载数据集[path]" blockType="command"
    //% path.shadow="string" path.defl=ChnSentiCorp_htl_all.csv
    export function myBlock3(parameter: any, block: any) {
        let path= parameter.path.code
        Generator.addCode(`analyzer.load_dataset(${path})`)
    }
    //% block="设置训练集和验证集的比例为[data]" blockType="command"
    //% data.shadow="dropdown" data.options="data" 
    export function myBlock4(parameter: any, block: any) {
        let data= parameter.data.code
        Generator.addCode(`analyzer.set_train_test_split(test_size=${data})`)
    }
    //% block="分词和构建词汇表，并保存分词结果" blockType="command"
    export function myBlock5(parameter: any, block: any) {
        Generator.addCode(`
analyzer.tokenize_and_build_vocab(save_tokenized=True)

`)
    }
    //% block="将文本转换为词向量序列" blockType="command"
    export function myBlock9(parameter: any, block: any) {
        Generator.addCode(`analyzer.convert_to_vectors()`)
    }
    //% block="训练模型" blockType="tag"
    export function tagmyBlock20() {
    }
    //% block="RNN模型" blockType="tag"
    export function tagmyBlock21() {
    }
    //% block="构建RNN模型，设置词向量的维度为[a]，隐藏层的维度为[b]，输出维度为[c]" blockType="command"
    //% a.shadow="number" a.defl=256
    //% b.shadow="number" b.defl=128
    //% c.shadow="number" c.defl=2
    export function myBlock6(parameter: any, block: any) {
        let a= parameter.a.code
        let b= parameter.b.code
        let c= parameter.c.code
        Generator.addCode(`analyzer.set_hyperparameters(
         embedding_dim=${a},
         hidden_dim=${b},
         output_dim=${c}
     )`)
    }
    //% block="设置训练轮次为[a]，学习率为[b]，数据批次为[c]" blockType="command"
    //% a.shadow="normal" a.defl=2
    //% b.shadow="normal" b.defl=0.001
    //% c.shadow="normal" c.defl=200
    export function myBlock7(parameter: any, block: any) {
        let a= parameter.a.code
        let b= parameter.b.code
        let c= parameter.c.code
        Generator.addCode(`analyzer.set_hyperparameters(
         batch_size=${c},
         learning_rate=${b},
         num_epochs=${a}
     )
`)
    }
    //% block="训练并评估RNN模型，保存模型到文件夹[path]" blockType="command"
    //% path.shadow="string" path.defl=saved_RNN_model
    export function myBlock8(parameter: any, block: any) {
        let path= parameter.path.code
        Generator.addCode(`analyzer.train_rnn_model(${path})`)
    }
    //% block="LSTM模型" blockType="tag"
    export function tagmyBlock23() {
    }
    //% block="构建LSTM模型，设置词向量的维度为[a]，隐藏层的维度为[b]，输出维度为[c]" blockType="command"
    //% a.shadow="number" a.defl=256
    //% b.shadow="number" b.defl=128
    //% c.shadow="number" c.defl=2
    export function myBlock16(parameter: any, block: any) {
        let a= parameter.a.code
        let b= parameter.b.code
        let c= parameter.c.code
        Generator.addCode(`analyzer.set_hyperparameters(
         embedding_dim=${a},
         hidden_dim=${b},
         output_dim=${c}
     )`)
    }
    //% block="设置训练轮次为[a]，学习率为[b]，数据批次为[c]" blockType="command"
    //% a.shadow="number" a.defl=2
    //% b.shadow="number" b.defl=0.001
    //% c.shadow="number" c.defl=200
    export function myBlock22(parameter: any, block: any) {
        let a= parameter.a.code
        let b= parameter.b.code
        let c= parameter.c.code
        Generator.addCode(`analyzer.set_hyperparameters(
         batch_size=${c},
         learning_rate=${b},
         num_epochs=${a}
     )
`)
    }
    //% block="训练并评估LSTM模型，保存模型到文件夹[path]" blockType="command"
    //% path.shadow="string" path.defl=saved_LSTM_model
    export function myBlock24(parameter: any, block: any) {
        let path= parameter.path.code
        Generator.addCode(`analyzer.train_lstm_model(${path})`)
    }
    //% block="使用模型推理" blockType="tag"
    export function tagmyBlock25() {
    }
    //% block="启动弹窗测试训练好的RNN模型,模型路径[data]" blockType="command"
    //% data.shadow="string" data.defl=saved_RNN_model
    export function myBlock10(parameter: any, block: any) {
        let data= parameter.data.code
        Generator.addCode(`if analyzer.load_RNN_model(
    model_path=${data}+"/model.pth",
    vocab_path=${data}+"/vocab.pth",
    config_path=${data}+"/config.pth"
):
    analyzer.start_gui()
else:
    print("无法加载模型，请检查路径是否正确")`)
    }
    //% block="启动弹窗测试训练好的LSTM模型,模型路径[data]" blockType="command"
    //% data.shadow="string" data.defl=saved_LSTM_model
    export function myBlock17(parameter: any, block: any) {
        let data= parameter.data.code
        Generator.addCode(`if analyzer.load_LSTM_model(
    model_path=${data}+"/model.pth",
    vocab_path=${data}+"/vocab.pth",
    config_path=${data}+"/config.pth"
):
    analyzer.start_gui()
else:
    print("无法加载模型，请检查路径是否正确")`)
    }
    //% block="设置文本标签为[data]" blockType="command"
    //% data.shadow="list" data.defl='"消极","积极"' 
    export function myBlock11(parameter: any, block: any) {
        let data= parameter.data.code
        Generator.addCode(`labels = ${data}
label_names = {}
label_names = {i: label for i, label in enumerate(labels)}`)
    }
    //% block="设置待推理的文本为[data]" blockType="command"
    //% data.shadow="string" data.defl=这家酒店的服务非常好，房间也很干净。
    export function myBlock12(parameter: any, block: any) {
        let data= parameter.data.code
        Generator.addCode(`text = ${data}`)
    }
    //% block="使用RNN模型[data]进行推理并保存结果[result]" blockType="command"
    //% data.shadow="string" data.defl=saved_RNN_model
    //% result.shadow="string" result.defl=predictions.csv
    export function myBlock26(parameter: any, block: any) {
        let data= parameter.data.code
        let result= parameter.result.code
        Generator.addCode(`predictor.load_RNN_model(model_path=${data}+"/model.pth",
    vocab_path=${data}+"/vocab.pth",
    config_path=${data}+"/config.pth")
 
result = predictor.predict(text, label_names)
print(f"文本: {result['text']}")
print(f"预测类别: {result['predicted_class']}")
print(f"置信度: {result['confidence']}%")
print(f"概率分布: {result['probabilities']}")
#保存预测结果
predictor.save_predictions(result, ${result})`)
    }
    //% block="使用LSTM模型[data]进行推理并保存结果[result]" blockType="command"
    //% data.shadow="string" data.defl=saved_LSTM_model
    //% result.shadow="string" result.defl=predictions.csv
    export function myBlock14(parameter: any, block: any) {
        let data= parameter.data.code
        let result= parameter.result.code
        Generator.addCode(`predictor.load_LSTM_model(model_path=${data}+"/model.pth",
    vocab_path=${data}+"/vocab.pth",
    config_path=${data}+"/config.pth")
 
result = predictor.predict(text, label_names)
print(f"文本: {result['text']}")
print(f"预测类别: {result['predicted_class']}")
print(f"置信度: {result['confidence']}%")
print(f"概率分布: {result['probabilities']}")
#保存预测结果
predictor.save_predictions(result, ${result})`)
    }
    //% block="推理结果[a]" blockType="reporter"
    //% a.shadow="dropdown" a.options="a" 
    export function myBlock15(parameter: any, block: any) {
        let a= parameter.a.code
        Generator.addCode(`result['${a}']
`)
    }
    }
    